package com.shjy.driver.application;


import android.app.ActivityManager;
import android.app.Application;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;

import com.shjy.driver.bordcast.MyBroadcastReceiver;
import com.shjy.driver.service.LocationService;

import java.util.Timer;
import java.util.TimerTask;

/**
 * @ClassName: MyApplication
 * @package com.mayi.application
 * @author mayi
 * @date 2014-8-28 下午11:51:30
 * @Description: TODO(这里用一句话描述这个类的作用)
 * @version 1.0
 *
 */

public class MyApplication extends Application {

    public boolean isServiceRunning = false;
    public Context context;
    @Override
    public void onCreate() {
        super.onCreate();

        context = this.getApplicationContext();
        //goService();

        IntentFilter filter = new IntentFilter(Intent.ACTION_TIME_TICK);
        MyBroadcastReceiver receiver = new MyBroadcastReceiver();
        registerReceiver(receiver, filter);
    }

    public void goService(){
        Timer timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                isServiceRunning = false;
                ActivityManager manager = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
                for (ActivityManager.RunningServiceInfo service : manager.getRunningServices(Integer.MAX_VALUE)) {
                    if ("com.shjy.driver.service.LocationService".equals(service.service.getClassName())) {
                        isServiceRunning = true;
                    }
                }
                if (!isServiceRunning) {
                    Intent i = new Intent(context, LocationService.class);
                    //stopService(i);
                    context.startService(i);

                }
            }
        },10000,20000);
    }

}